package com.bitin.adorn.modules.system.service.impl;

import com.bitin.adorn.modules.system.domain.Role;
import com.bitin.adorn.modules.system.domain.Employee;
import com.bitin.adorn.modules.system.repository.RoleRepository;
import com.bitin.adorn.modules.system.repository.EmployeeRepository;
import com.bitin.adorn.modules.system.service.EmployeeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Mr.Lu
 * @since 2024/5/30 15:56
 */
@Service
@RequiredArgsConstructor
public class EmployeeServiceImpl implements EmployeeService {

    private final EmployeeRepository employeeRepository;
    private final RoleRepository roleRepository;

    @Override
    public Employee init() {
//        Set<Permission> permissions = new HashSet<>();
//        for (int i = 0; i < 5; i++) {
//            permissions.add(new Permission((long) (1000 + i), String.format("权限%d", i)));
//        }

        Set<Role> roles = new HashSet<>();
        for (int i = 0; i < 2; i++) {
            roles.add(new Role((long) (2000 + i), String.format("角色%d", i)));
        }

        Employee user = new Employee(3000L, "user", "password", "", roles);
        return employeeRepository.save(user);
    }

    @Override
    public Employee save(Employee user) {
        return employeeRepository.save(user);
    }

    @Override
    public List<Employee> list() {
        return employeeRepository.findAll();
    }

    @Override
    public Employee findById(Long id) {
        return employeeRepository.findById(id).orElseThrow();
    }

    @Override
    public void deleteById(Long id) {
        employeeRepository.deleteById(id);
    }

    @Override
    public Employee bind(Long id, Long roleId) {
        Employee user = employeeRepository.findById(id).orElseThrow();
        Role role = roleRepository.findById(roleId).orElseThrow();
        return employeeRepository.save(user.bind(role));
    }

}
